<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入内容" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-popconfirm
        class="ml-5"
        confirm-button-text='确定'
        cancel-button-text='我再想想'
        icon="el-icon-info"
        icon-color="red"
        title="您确定批量删除这些数据吗？"
        @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="content" label="留言内容"></el-table-column>
      <el-table-column prop="user" label="留言人"></el-table-column>
      <el-table-column prop="time" label="留言时间"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="warning">未回复</el-tag>
          <el-tag v-else type="success">已回复</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleReply(scope.row)">回复</el-button>
          <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon="el-icon-info"
            icon-color="red"
            title="您确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog title="留言回复" :visible.sync="dialogFormVisible" width="40%">
      <el-form label-width="100px" style="width: 90%">
        <el-form-item label="留言内容">
          <div style="background-color: #f5f7fa; padding: 10px; border-radius: 4px;">
            {{ form.content }}
          </div>
        </el-form-item>
        <el-form-item label="留言人">
          <div>{{ form.user }}</div>
        </el-form-item>
        <el-form-item label="留言时间">
          <div>{{ form.time }}</div>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input type="textarea" v-model="form.reply" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'

export default {
  name: "Message",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: []
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/message/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleReply(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    save() {
      if (!this.form.reply) {
        this.$message.warning("请输入回复内容");
        return;
      }
      this.form.status = '1';  // 已回复状态
      this.form.replyTime = moment().format('YYYY-MM-DD HH:mm:ss');
      
      this.request.post("/message", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("回复成功");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("回复失败");
        }
      });
    },
    del(id) {
      this.request.delete("/message/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.warning("请选择需要删除的数据");
        return;
      }
      
      let ids = this.multipleSelection.map(v => v.id);
      this.request.post("/message/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功");
          this.load();
        } else {
          this.$message.error("批量删除失败");
        }
      });
    },
    reset() {
      this.name = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    }
  }
};
</script>

<style scoped>
.headerBg {
  background-color: #eee !important;
}
</style>